package com.hnevc.myspeaker.Shopping;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.hnevc.myspeaker.R;
import com.hnevc.myspeaker.entity.Product;


public class ProductActivity extends AppCompatActivity {
    Product product;
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);
        initData();
        initView();
        setToolBar();
    }

    private void initView() {
        toolbar = findViewById(R.id.action_bar);
        ImageView ivProductPic =findViewById(R.id.iv_product_pic);

        TextView tvProductName =findViewById(R.id.tv_product_name);
        TextView tvProductDesc = findViewById(R.id.tv_product_description);
        TextView tvProductPrice = findViewById(R.id.tv_product_price);


        ivProductPic.setImageResource(getImageIdentify(product));
        tvProductName.setText(product.getProductName());
        tvProductDesc.setText(product.getDescription());
        tvProductPrice.setText("价格:"+product.getPrice()+"元");
    }

    private void initData() {
        Intent intent = getIntent();
        product = (Product) intent.getSerializableExtra("product");
    }
    private int getImageIdentify(Product product) {
        int identifier = getResources()
                .getIdentifier(product.getProductPic().split("\\.")[0]
                        , "mipmap", getPackageName());
        return identifier;
    }

    private void setToolBar() {
        setSupportActionBar(toolbar);
        View view = LayoutInflater.from(this).inflate(R.layout.toolbar_layout, null);
        ActionBar actionBar = getSupportActionBar();
        ActionBar.LayoutParams params = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.MATCH_PARENT,
                ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER
        );
        actionBar.setCustomView(view, params);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        TextView textView = view.findViewById(R.id.textView_main);
        textView.setText("商品详细--"+product.getProductName());
        ImageView imageView = view.findViewById(R.id.menu_main);
        imageView.setImageResource(R.mipmap.back);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
